/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.score.controller;

import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.score.entity.Score;
import vip.xiaonuo.modular.score.mapper.ScoreMapper;
import vip.xiaonuo.modular.score.param.ScoreParam;
import vip.xiaonuo.modular.score.service.ScoreService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;

/**
 * 分数管理控制器
 *
 * @author zls
 * @date 2022-07-05 16:01:25
 */
@Controller
public class ScoreController {

    private String PATH_PREFIX = "score/";


    @Autowired
    public ScoreMapper scoreMapper;

    @Resource
    private ScoreService scoreService;

    /**
     * 分数管理页面
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @Permission
    @GetMapping("/score/index")
    public String index() {
        return PATH_PREFIX + "index.html";
    }

    /**
     * 分数管理表单页面
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @GetMapping("/score/form")
    public String form() {
        return PATH_PREFIX + "form.html";
    }

    /**
     * 查询分数管理
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @ResponseBody
    @GetMapping("/score/page")
    @BusinessLog(title = "分数管理_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public PageResult<Score> page(ScoreParam scoreParam) {
        return scoreService.page(scoreParam);
    }

    /**
     * 添加分数管理
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */

    @ResponseBody
    @PostMapping("/score/add")
    @BusinessLog(title = "分数管理_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(ScoreParam.add.class) ScoreParam scoreParam) {
        scoreService.add(scoreParam);
        return new SuccessResponseData();
    }

    @ResponseBody
    @PostMapping("/score/jiesuan")
    public ResponseData jiesuan(@RequestBody ScoreParam scoreParam) {
        Score lastRecord = scoreService.getLastRecord();
        Integer score = scoreParam.getScore();
        Integer dong = lastRecord.getDong();
        Integer xi = lastRecord.getXi();
        Integer nan = lastRecord.getNan();
        Integer bei = lastRecord.getBei();
        switch (scoreParam.getPosition()) {
            case "dong":
                dong += (score*3);
                xi -= score;
                nan -= score;
                bei -= score;
                scoreParam.setDong(dong);
                scoreParam.setXi(xi);
                scoreParam.setNan(nan);
                scoreParam.setBei(bei);
                scoreService.add(scoreParam);
                break;
            case "xi":
                dong -= score;
                xi += (score*3);
                nan -= score;
                bei -= score;
                scoreParam.setDong(dong);
                scoreParam.setXi(xi);
                scoreParam.setNan(nan);
                scoreParam.setBei(bei);
                scoreService.add(scoreParam);
                break;
            case "nan":
                dong -= score;
                xi -= score;
                nan += (score*3);
                bei -= score;
                scoreParam.setDong(dong);
                scoreParam.setXi(xi);
                scoreParam.setNan(nan);
                scoreParam.setBei(bei);
                scoreService.add(scoreParam);
                break;
            case "bei":
                dong -= score;
                xi -= score;
                nan -= score;
                bei += (score*3);
                scoreParam.setDong(dong);
                scoreParam.setXi(xi);
                scoreParam.setNan(nan);
                scoreParam.setBei(bei);
                scoreService.add(scoreParam);
                break;
        }
        Score lastRecord1 = scoreService.getLastRecord();
        if (scoreParam.getMing().size()!=0){
            for (String name:scoreParam.getMing()) {
                scoreMapper.minggang(name,lastRecord1.getId());
            }
        }
        if (scoreParam.getAn().size()!=0){
            for (String name:scoreParam.getAn()) {
                scoreMapper.angang(name,lastRecord1.getId());
            }
        }
        return new SuccessResponseData();
    }


    @ResponseBody
    @PostMapping("/score/diaopao")
    public ResponseData diaopao(@RequestBody ScoreParam scoreParam) {
        Score lastRecord = scoreService.getLastRecord();
        Integer score = scoreParam.getScore();
        Integer dong = lastRecord.getDong();
        Integer xi = lastRecord.getXi();
        Integer nan = lastRecord.getNan();
        Integer bei = lastRecord.getBei();

        switch (scoreParam.getPosition()) {
            case "dong":
                dong += score;
                ScoreParam dianpao = getDianpao(scoreParam, dong, xi, nan, bei, score, scoreParam.getDianpao());
                scoreService.add(dianpao);
                break;
            case "xi":
                xi += score;
                ScoreParam dianpao1 = getDianpao(scoreParam, dong, xi, nan, bei, score, scoreParam.getDianpao());
                scoreService.add(dianpao1);
                break;
            case "nan":
                nan += score;
                ScoreParam dianpao2 = getDianpao(scoreParam, dong, xi, nan, bei, score, scoreParam.getDianpao());
                scoreService.add(dianpao2);
                break;
            case "bei":
                bei += score;
                ScoreParam dianpao3 = getDianpao(scoreParam, dong, xi, nan, bei, score, scoreParam.getDianpao());
                scoreService.add(dianpao3);
                break;
        }
        Score lastRecord1 = scoreService.getLastRecord();
        if (scoreParam.getMing().size()!=0){
            for (String name:scoreParam.getMing()) {
                scoreMapper.minggang(name,lastRecord1.getId());
            }
        }
        if (scoreParam.getAn().size()!=0){
            for (String name:scoreParam.getAn()) {
                scoreMapper.angang(name,lastRecord1.getId());
            }
        }
        return new SuccessResponseData();
    }

    public ScoreParam getDianpao(ScoreParam scoreParam,Integer dong,Integer xi,Integer nan,Integer bei,Integer score,String dianpao){
        switch (dianpao){
            case "dong":
                dong -= score;
                break;
            case "xi":
                xi -= score;
                break;
            case "nan":
                nan -= score;
                break;
            case "bei":
                bei -= score;
                break;
        }
            scoreParam.setDong(dong);
            scoreParam.setXi(xi);
            scoreParam.setNan(nan);
            scoreParam.setBei(bei);

        return scoreParam;
    }
    /**
     * 删除分数管理
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @Permission
    @ResponseBody
    @PostMapping("/score/delete")
    @BusinessLog(title = "分数管理_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(ScoreParam.delete.class) List<ScoreParam> scoreParamList) {
        scoreService.delete(scoreParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑分数管理
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @Permission
    @ResponseBody
    @PostMapping("/score/edit")
    @BusinessLog(title = "分数管理_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(ScoreParam.edit.class) ScoreParam scoreParam) {
        scoreService.edit(scoreParam);
        return new SuccessResponseData();
    }

    /**
     * 查看分数管理
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @Permission
    @ResponseBody
    @GetMapping("/score/detail")
    @BusinessLog(title = "分数管理_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(ScoreParam.detail.class) ScoreParam scoreParam) {
        return new SuccessResponseData(scoreService.detail(scoreParam));
    }

    /**
     * 分数管理列表
     *
     * @author zls
     * @date 2022-07-05 16:01:25
     */
    @Permission
    @ResponseBody
    @GetMapping("/score/list")
    @BusinessLog(title = "分数管理_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(ScoreParam scoreParam) {
        return new SuccessResponseData(scoreService.list(scoreParam));
    }

}
